package com.example.medical_project.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.medical_project.commons.vo.AjaxResult;
import com.example.medical_project.modules.system.domain.DictData;
import com.example.medical_project.modules.system.domain.DictType;
import com.example.medical_project.modules.system.dto.DictTypeDto;
import com.example.medical_project.modules.system.service.DictTypeService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/system/dict/type")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;


//    获取字典类型列表
    @GetMapping("/listForPage")
    public AjaxResult listForPage(DictTypeDto dictTypeDto){

        Page<DictType> page = dictTypeService.listForPage(dictTypeDto);

        return AjaxResult.success(page.getRecords(), page.getTotal());

    }

//    获取所有字典类型列表
    @GetMapping("/selectAllDictType")
    public AjaxResult selectAllDictType(){

        List<DictType> dictTypes = dictTypeService.selectAllDictType();

        return AjaxResult.success(dictTypes);

    }


    /**
     * 根据typeId查询字典数据
     * @param typeId 字典id
     * @return
     */
    @GetMapping("/getOne/{typeId}")
    public AjaxResult getDictType(@PathVariable @Validated @NotNull(message = "数据字典类型id不能为空") Long typeId){

        DictType byId = dictTypeService.getById(typeId);

        return AjaxResult.success(byId);
    }


    /**
     * 同步MYSQL中的数据缓存到redis中减少与数据库的交互加快数据查询效率
     */

    @GetMapping("/dictCacheAsync")
    public AjaxResult dictCacheAsync(){

        dictTypeService.dictCacheAsync();

        return AjaxResult.success();

    }





}
